package com.alibaba.cirtus.ots.application.app.ability.facade.v2serviceimpl;

import com.alibaba.cz.base.tool.log.Log;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.lattice2.epoch.sdk.model.Result;
import com.epoch.app.bcorder.model.dto.QueryGoodsListForForecastSaleRequest;
import com.epoch.app.bcorder.model.dto.QueryGoodsListRequest;
import com.epoch.app.bcorder.model.dto.QueryGoodsListResponse;
import com.epoch.app.bcorder.service.PlatformEnhanceService;
import com.epoch.app.otsapplicationappsdk.favorites.dto.InitRequest;
import com.epoch.app.otsapplicationappsdk.favorites.dto.SubmitRequest;
import com.epoch.app.otsapplicationappsdk.favorites.service.FavoritesService;
import com.epoch.app.crplatformenhance.goodsfavorites.dto.QueryGoodsFavoritesRequest;
import com.epoch.app.crplatformenhance.goodsfavorites.model.GoodsFavorites;
import com.epoch.app.crplatformenhance.goodsfavorites.service.GoodsFavoritesService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

@Service
@Primary
public class FavoritesV2ServiceImpl implements FavoritesService {
    @Resource
    private GoodsFavoritesService favoritesService;

    @Resource
    PlatformEnhanceService platformEnhanceService;

    private static Log log = Log.getLogger(FavoritesV2ServiceImpl.class);

    @Override
    public  Object init(InitRequest initRequest){
        JSONObject result = new JSONObject();
        result.put("success",true);
        //我的收藏货品
        try {
            Result<List<GoodsFavorites>> listResult = favoritesService.queryGoodsFavoritesForForecastSale();
            if (Objects.nonNull(listResult)&& CollectionUtils.isNotEmpty(listResult.getResult())){
                result.put("GoodsFavorites",listResult.getResult());
            }
        } catch (Exception e) {
            log.info("我的收藏渲染页 获取收藏API异常");
            e.printStackTrace();
            result.put("message","收藏页面异常");
            result.put("success",false);
            return result;
        }

        //货品明细
        try {
            QueryGoodsListForForecastSaleRequest queryGoodsListRequest = new QueryGoodsListForForecastSaleRequest();
            queryGoodsListRequest.setRecentlyUsedItemIdList(initRequest.getGoodsIdList());
            Result<List<QueryGoodsListResponse>> queryGoodsListResponse = platformEnhanceService.queryGoodsListForForecastSale(queryGoodsListRequest);
            if (Objects.nonNull(queryGoodsListResponse)&& CollectionUtils.isNotEmpty(queryGoodsListResponse.getResult())){
                result.put("ScItemGoodsList",queryGoodsListResponse.getResult());
            }
        } catch (Exception e) {
            log.info("我的收藏渲染页 获取货品明细API异常");
            e.printStackTrace();
            result.put("message","货品明细异常");
            result.put("success",false);
            return result;
        }

        return result;
    }

    @Override
    public Object submit(SubmitRequest submitRequest){
        log.info("收藏夹提交 请求参数 {} " , JSON.toJSONString(submitRequest));
        JSONObject result = new JSONObject();
        result.put("success", true);
        try {
            JSONObject request = (JSONObject) JSONObject.toJSON(submitRequest.getRequest());
            JSONObject submitFields = request.getJSONObject("fields").getJSONObject("submitFields");
            if (submitFields.get("submitGoods") != null) {
                GoodsFavorites goodsFavorites = new GoodsFavorites();
                goodsFavorites.setGoodsList(JSON.toJSONString(submitFields.get("submitGoods")));
                if (submitFields.get("id") != null) {
                    //编辑(删除，添加)
                    goodsFavorites.setId(submitFields.getLong("id"));
                }
                Result<Boolean> favoritesResult = favoritesService.updateOrInsertFavorites(goodsFavorites);
                log.info("收藏页面提交结果 ：" + JSON.toJSONString(favoritesResult));
            }

        } catch (Exception e) {
            log.info("收藏页面提交异常！");
            e.printStackTrace();
            result.put("success",false);
            result.put("message","提交异常");
        }

        return result;
    }

}
